package com.example.app23_graphics;

import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MatrixActivity extends AppCompatActivity {

    private EditText et_matrix_scale;
    private EditText et_matrix_rotate;
    private EditText et_matrix_translateX;
    private EditText et_matrix_translateY;
    private ImageView iv_matrix_icon;

    private Matrix matrix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_matrix);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.ll_matrix), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        et_matrix_scale = findViewById(R.id.et_matrix_scale);
        et_matrix_rotate = findViewById(R.id.et_matrix_rotate);
        et_matrix_translateX = findViewById(R.id.et_matrix_translateX);
        et_matrix_translateY = findViewById(R.id.getEt_matrix_translateY);
        iv_matrix_icon = findViewById(R.id.iv_matrix_icon);

        matrix = new Matrix();
    }

    /**
     * 缩放
     * @param view v
     */
    public void scaleBitmap(View view) {
        float scale = Float.parseFloat(et_matrix_scale.getText().toString());
        matrix.postScale(scale, scale);
        // 将 matrix 设置到 ImageView
        // 需要设置 ImageView 的 android:scaleType="matrix"
        iv_matrix_icon.setImageMatrix(matrix);
    }

    /**
     * 旋转
     * @param view v
     */
    public void rotateBitmap(View view) {
        float degree = Float.parseFloat(et_matrix_rotate.getText().toString());
        matrix.postRotate(degree);
        //
        iv_matrix_icon.setImageMatrix(matrix);
    }

    public void translateBitmap(View view) {
        float dx = Float.parseFloat(et_matrix_translateX.getText().toString());
        float dy = Float.parseFloat(et_matrix_translateY.getText().toString());
        matrix.postTranslate(dx, dy);
        //
        iv_matrix_icon.setImageMatrix(matrix);
    }

    public void clearMatrix(View view) {
        matrix.reset();
        iv_matrix_icon.setImageMatrix(matrix);
    }
}